package com.org.cloud.lg.utils;
import com.org.cloud.lg.constants.SecurityConstants;
import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName: BaseContextKit
 * @Author: LG
 * @Date: 2019/4/30 15:40
 * @Version: 1.0
 **/
public class BaseContextKit {

    public static ThreadLocal<Map<String, Object>> threadLocal = new ThreadLocal<Map<String, Object>>();

    public static void set(String key, Object value) {
        Map<String, Object> map = threadLocal.get();
        if (map == null) {
            map = new HashMap<String, Object>();
            threadLocal.set(map);
        }
        map.put(key, value);
    }

    public static Object get(String key){
        Map<String, Object> map = threadLocal.get();
        if (map == null) {
            map = new HashMap<String, Object>();
            threadLocal.set(map);
        }
        return map.get(key) == null ? "" : map.get(key);
    }

    public static String getUserID(){
        Object value = get(SecurityConstants.USER_ID);
        String res=returnObjectValue(value);
        return res;
    }

    public static Long getDeptID(){
        Object value = get(SecurityConstants.DEPT_ID);
        String res=returnObjectValue(value);
        return Checker.BeBlank(res)?null:Long.valueOf(res);
    }


    public static String getName(){
        Object value = get(SecurityConstants.CONTEXT_KEY_FULL_NAME);
        return (String)value;
    }

    public static String getToken(){
        Object value = get(SecurityConstants.CONTEXT_KEY_TOKEN);
        return (String)value;
    }

    public static String getRoles(){
        Object value = get(SecurityConstants.CONTEXT_KEY_API_ROLES);
        return (String)value;
    }
    public static void setToken(String token){set(SecurityConstants.CONTEXT_KEY_TOKEN,token);}

    public static void setFullName(String name){set(SecurityConstants.CONTEXT_KEY_FULL_NAME,name);}

    public static void setDeptID(String groupID){set(SecurityConstants.DEPT_ID,groupID);}

    public static void setUserID(String userID){
        set(SecurityConstants.USER_ID,userID);
    }

    public static void setRoles(String roles){
        set(SecurityConstants.CONTEXT_KEY_API_ROLES,roles);
    }

    private static String returnObjectValue(Object value) {
        return value==null?null:value.toString();
    }

    public static void remove(){
        threadLocal.remove();
    }
}
